<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/9/21
 * Time: 13:30
 */

namespace Wechat\Controller;

class WebzhuanController extends CommonController
{
    //其他转介-装修
    public function qita()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['tel']))) {
                $this->error('请输入相关业务手机号');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入发布者手机号');
            }
            if(trim(empty($post['des']))) {
                $this->error('请输入详细描述');
            }
            if(trim(empty($post['name']))) {
                $this->error('请输入发布人姓名');
            }
            if(trim(empty($post['compname']))) {
                $this->error('请输入公司名称');
            }
            $data = [
                'type'=>1,
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'compname'=>$post['compname'],
                'fenyong'=>$post['fenyong'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'tel'=>$post['tel'],
                'compthumb'=>$post['compthumb'],
                'phone'=>$post['phone'],
                'read'=>0,
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid'],
                'data'=>'qita'
            ];
            if(M('zhuanqita')->add($data)){
                $this->success('发布成功！',U('Webzhuan/qita'));
            }else{
                $this->success('失败！！');
            }
        }else{
            //1为装修 2为搬家
            //获取装修数据
            $zhuangxiu = M('zhuanqita')->order('id desc')->where(['type'=>1])->select();
            foreach ($zhuangxiu as $k =>$v){
                $zhuangxiu[$k]['thumb'] = explode('|',$v['thumb']);
            }
            //获取搬家数据
            $banjia = M('zhuanqita')->order('id desc')->where(['type'=>2])->select();
            foreach ($banjia as $k =>$v){
                $banjia[$k]['thumb'] = explode('|',$v['thumb']);
            }
            //获取其他转介轮播图
            $lun = M('slide')->where(['slide_cid'=>4])->select();
            $this->assign(['lun'=>$lun,'zhuangxiu'=>$zhuangxiu,'banjia'=>$banjia]);
            $this->display();
        }
    }
    //其他转介-搬家
    public function banjia()
    {
        $post = i('post.');
        if(trim(empty($post['quyu']))) {
            $this->error('请选择区域');
        }
        if(trim(empty($post['thumb']))) {
            $this->error('请上传相关图片内容');
        }
        if(trim(empty($post['tel']))) {
            $this->error('请输入相关业务手机号');
        }
        if(trim(empty($post['phone']))) {
            $this->error('请输入发布者手机号');
        }
        if(trim(empty($post['des']))) {
            $this->error('请输入详细描述');
        }
        if(trim(empty($post['name']))) {
            $this->error('请输入发布人姓名');
        }
        if(trim(empty($post['compname']))) {
            $this->error('请输入公司名称');
        }
        if(trim(empty($post['compthumb']))) {
            $this->error('请上传公司营业执照');
        }
        $data = [
            'type'=>2,
            'chengshi'=>$post['chengshi'],
            'quyu'=>$post['quyu'],
            'thumb'=>$post['thumb'],
            'des'=>$post['des'],
            'compname'=>$post['compname'],
            'fenyong'=>$post['fenyong'],
            'name'=>$post['name'],
            'erwei'=>$post['erwei'],
            'tel'=>$post['tel'],
            'phone'=>$post['phone'],
            'compthumb'=>$post['compthumb'],
            'read'=>0,
            'time'=>time(),
            'openid'=>$_SESSION['wechatopenid'],
            'data'=>'qita'
        ];
        if(M('zhuanqita')->add($data)){
            $this->success('发布成功！',U('Webzhuan/banjia'));
        }else{
            $this->success('失败！！');
        }
    }
    //七牛
    public function ajax()
    {
        $setting = [
            'maxSize' => 10 * 1024 * 1024,//文件大小
            'rootPath' => './data/',
            'savePath'=> 'upload/wechat/',
            'saveName' => array ('uniqid', ''),
            'driver' => 'Qiniu',
            'driverConfig' => array (
                'secretKey' => 'EU7XnbTRj3YY8n7lpb4gGDpOIHvSoIcMr3sVYiyr',
                'accessKey' => 'lmavbdsquJ7hboYBWH8SjDT8FZrS5YyE5QNxwnAk',
                'domain' => 'ow5gczjvj.bkt.clouddn.com',
                'bucket' => 'xiaoniao',
            )
        ];

        $Upload = new \Think\Upload($setting);

        $info = $Upload->upload($_FILES);

        if(!$info) {// 上传错误提示错误信息
            $this->error($Upload->getError());
        }
        $imgs = "";

        foreach($info as $file){
            //多图上传
            $imgs .= $file['url'].'|';
        }
        echo $imgs;
    }
    //项目
    public function xiangmu()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['thumb']))) {
                $this->error('请上传相关图片内容');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入发布者手机号');
            }
            if(trim(empty($post['des']))) {
                $this->error('请输入详细描述');
            }
            if(trim(empty($post['name']))) {
                $this->error('请输入发布人姓名');
            }
            //build 团队1 项目2
            $data=[
                'build'=>$post['build'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'renshu'=>$post['renshu'],
                'thumb'=>$post['thumb'],
                'phone'=>$post['phone'],
                'time'=>time(),
                'des'=>$post['des'],
                'type'=>$post['type'],
                'name'=>$post['name'],
                'zhuchang'=>$post['zhuchang'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'jiage'=>$post['jiage'],
                'ticheng'=>$post['ticheng'],
                'fanyong'=>$post['fanyong'],
                'read'=>0,
                'openid'=>$_SESSION['wechatopenid'],
                'data'=>'xiangmu'
            ];
            if(M('zhuanxiangmu')->add($data)){
                $this->success('发布成功！',U('Webzhuan/xiangmu'));
            }else{
                $this->error('失败，未知错误！');
            }
        }else{
            //获取项目转介轮播图
            $lun = M('slide')->where(['slide_cid'=>5])->select();
            $this->assign(['lun'=>$lun]);
            $this->display();
        }
    }
    //项目筛选
    public function xiangmufan()
    {
        $post = i('post.');
        $where = [
            'build'=>$post['build'],
            'type'=>$post['type'],
            'chengshi'=>$post['chengshi'],
            'quyu'=>$post['quyu'],
        ];

        foreach ($where as $k => $v){
            if(!$v){
                unset($where[$k]);
            }
        }

        $res = M('zhuanxiangmu')->order(['id'=>'desc','sort'=>'desc'])->where($where)->select();
        echo json_encode($res);
    }
    //租赁
    public function zulin()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['des']))) {
                $this->error('请填写描述');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'build'=>$post['build'],
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'huxing'=>$post['huxing'],
                'fangling'=>$post['fangling'],
                'louceng'=>$post['louceng'],
                'mianji'=>$post['mianji'],
                'zhuangxiu'=>$post['zhuangxiu'],
                'yuezu'=>$post['yuezu'],
                'fukuan'=>$post['fukuan'],
                'fenyong'=>$post['fenyong'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'phone'=>$post['phone'],
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid'],
                'read'=>0,
                'where'=>$post['where'],
                'where1'=>$post['where1'],
                'headimg'=>$user['headimg'],
                'data'=>'zulin'
            ];
            if(M('zhuanzulin')->add($data)){
                $this->success('发布成功！',U('Webzhuan/zulin'));
            }else{
                $this->error('发布失败！');
            }
        }else{
            $lun = M('slide')->where(['slide_cid'=>1])->select();
            $this->assign(['lun'=>$lun]);
            $this->display();
        }
    }
    //二手房
    public function ershou()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['des']))) {
                $this->error('请填写描述');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'build'=>$post['build'],
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'huxing'=>$post['huxing'],
                'fangling'=>$post['fangling'],
                'louceng'=>$post['louceng'],
                'mianji'=>$post['mianji'],
                'zhuangxiu'=>$post['zhuangxiu'],
                'jiage'=>$post['jiage'],
                'fenyong'=>$post['fenyong'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'erwei'=>$post['erwei'],
                'phone'=>$post['phone'],
                'time'=>time(),
                'openid'=>$_SESSION['wechatopenid'],
                'read'=>0,
                'where'=>$post['where'],
                'where1'=>$post['where1'],
                'headimg'=>$user['headimg'],
                'data'=>'ershou'
            ];
            if(M('zhuanershou')->add($data)){
                $this->success('发布成功！',U('Webzhuan/ershou'));
            }else{
                $this->error('发布失败！');
            }
        }else{
            $lun = M('slide')->where(['slide_cid'=>2])->select();
            $this->assign(['lun'=>$lun]);
            $this->display();
        }
    }
    //二手返回
    public function ershoufan()
    {
        $post = i('post.');
        $where = [
            'build'=>$post['build'],
            'type'=>$post['type'],
            'chengshi'=>$post['chengshi'],
            'quyu'=>$post['quyu'],
            'huxing'=>$post['huxing'],
            'fangling'=>$post['fangling'],
            'louceng'=>$post['louceng'],
            'mianji'=>$post['mianji'],
            'zhuangxiu'=>$post['zhuangxiu'],
            'where'=>$post['where'],//租金
            'where1'=>$post['where1'],//面积
            'fenyong'=>$post['fenyong'],
        ];

        foreach ($where as $k => $v){
            if(!$v){
                unset($where[$k]);
            }
        }

        //array_filter($where);
        $res = M('zhuanershou')->order(['id'=>'desc','sort'=>'desc'])->where($where)->select();
        echo json_encode($res);
    }
    //租赁返回数据
    public function zulinfan()
    {
        $post = i('post.');
        $where = [
            'build'=>$post['build'],
            'type'=>$post['type'],
            'chengshi'=>$post['chengshi'],
            'quyu'=>$post['quyu'],
            'huxing'=>$post['huxing'],
            'fangling'=>$post['fangling'],
            'louceng'=>$post['louceng'],
            'mianji'=>$post['mianji'],
            'zhuangxiu'=>$post['zhuangxiu'],
            'where'=>$post['where'],//租金
            'where1'=>$post['where1'],//面积
            'fenyong'=>$post['fenyong'],
        ];

        foreach ($where as $k => $v){
            if(!$v){
                unset($where[$k]);
            }
        }

        //array_filter($where);
        $res = M('zhuanzulin')->order(['id'=>'desc','sort'=>'desc'])->where($where)->select();
        echo json_encode($res);
    }
    //新房转介
    public function xinfang()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['buildname']))) {
                $this->error('请填写小区名称/商圈');
            }
            if(trim(empty($post['des']))) {
                $this->error('请填写描述');
            }
            if(trim(empty($post['thumb']))) {
                $this->error('请上传图片');
            }
            if(trim(empty($post['tel']))) {
                $this->error('请输入业务联系号码');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            if(trim(empty($post['jiage']))) {
                $this->error('请输入价格');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'type'=>$post['type'],
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'buildname'=>$post['buildname'],
                'dizhi'=>$post['dizhi'],
                'jiage'=>$post['jiage'],
                'yongjin'=>$post['yongjin'],
                'fanyong'=>$post['fanyong'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'name'=>$post['name'],
                'mianji'=>$post['mianji'],
                'erwei'=>$post['erwei'],
                'tel'=>$post['tel'],
                'phone'=>$post['phone'],
                'read'=>0,
                'time'=>time(),
                'headimg'=>$user['headimg'],
                'where'=>$post['where'],
                'openid'=>$_SESSION['wechatopenid'],
                'data'=>'xinfang'
            ];
            if(M('zhuanxinfang')->add($data)){
                $this->success('发布成功！',U('Webzhuan/xinfang'));
            }else{
                $this->error('失败！');
            }
        }else{
            $lun = M('slide')->where(['slide_cid'=>3])->select();
            $this->assign(['lun'=>$lun]);
            $this->display();
        }
    }
    //新房数据借接口
    public function xinfangshuju()
    {
        $data = M('zhuanxinfang')->order('time desc')->select();
        echo json_encode($data);
    }
    //新房筛选
    public function xinfangfan()
    {
        $post = i('post.');
        $where = [
            'chengshi'=>$post['chengshi'],
            'type'=>$post['type'],
            'quyu'=>$post['quyu'],
            'where'=>$post['where'],
        ];

        foreach ($where as $k => $v){
            if(!$v){
                unset($where[$k]);
            }
        }
        $res = M('zhuanxinfang')->order(['id'=>'desc','sort'=>'desc'])->where($where)->select();

        echo json_encode($res);
    }
    //单位招将
    public function zhaojiang()
    {
        if(IS_POST){
            $post = i('post.');
            if(trim(empty($post['mingcheng']))) {
                $this->error('请填写岗位名称');
            }
            if(trim(empty($post['phone']))) {
                $this->error('请输入手机号');
            }
            $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
            $data = [
                'chengshi'=>$post['chengshi'],
                'quyu'=>$post['quyu'],
                'yingye'=>$post['yingye'],
                'mingcheng'=>$post['mingcheng'],
                'renshu'=>$post['renshu'],
                'thumb'=>$post['thumb'],
                'des'=>$post['des'],
                'erwei'=>$post['erwei'],
                'phone'=>$post['phone'],
                'name'=>$post['name'],
                'compname'=>$post['compname'],
                'time'=>time(),
                'read'=>0,
                'openid'=>$_SESSION['wechatopenid'],
                'headimg'=>$user['headimg'],
                'data'=>'danwei'
            ];
            if(M('zhuandanwei')->add($data)){
                $this->success('发布成功！',U('Webzhuan/zhaojiang'));
            }else{
                $this->error('发布失败！');
            }
        }else{
            $lun = M('slide')->where(['slide_cid'=>7])->select();
            $this->assign(['lun'=>$lun]);
            $this->display();
        }
    }
    //招将返回数据
    public function zhaojiangfan()
    {
        $data = M('zhuandanwei')->order(['id'=>'desc','sort'=>'desc'])->select();
        echo json_encode($data);
    }
}